<?php

namespace app\admin\controller\data;

use app\admin\model\Shopdaytrade;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoptableorder;
use app\common\controller\Backend;

class

Shopmonthdata extends Backend
{

    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shopdaytrade;

    }

    public function index()
    {

        if ($this->request->isAjax()) {

            $date = $this->request->param('date');
            $shop_id = $this->request->param('shop_id');

            list($st, $et) = explode(' - ', $date);

            //根据时间戳和店铺，查询q_shop_day_trade 表的数据
            $where = array();
            $where['create_time'] = array("between", array(strtotime($st), strtotime($et)));
            $where['shop_id'] = $shop_id;

            //查询收益表
            $profit = new Shoporderprofit();
            $profitList = $profit->where($where)
                ->field('DATE_FORMAT(FROM_UNIXTIME(create_time), "%m-%d") AS date,sum(amount) as amount')
                ->group('date')
                ->select();

            $profitDateArr = array();
            $profitDataArr = array();
            foreach ($profitList as $v){
                $profitDateArr[] = $v['date'];
                $profitDataArr[] = $v['amount'];
            }
            $data['x_profit']=$profitDateArr;
            $data['y_profit']=$profitDataArr;

            //查询日统计表
            $list = $this->model
                ->where($where)
                ->field('DATE_FORMAT(FROM_UNIXTIME(create_time), "%m-%d") AS date,book_amount,book_pay_amount,charge_amount,tuan_amount,dou_amount,mt_amount,profit,balance_amount,tq_amount')
                ->select();

            //var_dump($list);
            $dateArr = array();
            $qp = array();
            $tq = array();
            $charge = array();
            $tuan = array();
            $dou = array();
            $mt = array();
            $profit = array();
            $balance = array();
            $total = array();

            foreach ($list as $v) {
                $dateArr[] = $v['date'];
                $qp[] = $v['book_amount'];
                $tq[] = $v['tq_amount'];
                $charge[] = $v['charge_amount'];
                $tuan[] = $v['tuan_amount'];
                $dou[] = $v['dou_amount'];
                $mt[] = $v['mt_amount'];
                $profit[] = $v['profit'];
                $balance[] = $v['balance_amount'];
                $total[] = ceil(($v['book_amount'] + $v['tq_amount'] + $v['charge_amount'] + $v['tuan_amount'] - $v['balance_amount']) * 100) / 100;
            }

            $newData = array();
            $newData['qp'] = $qp;
            $newData['tq'] = $tq;
            $newData['charge'] = $charge;
            $newData['tuan'] = $tuan;
            $newData['dou'] = $dou;
            $newData['mt'] = $mt;
            $newData['profit'] = $profit;
            $newData['balance'] = $balance;
            $newData['total'] = $total;

            $data['x'] = $dateArr;
            $data['y'] = $newData;

            //查询本月-棋牌-流水订单
            $qpOrder = new Shoporder();
            $qpList = $qpOrder->where($where)->where('status', 'in', ['payed', 'doing', 'done'])->select();

            $qpAmountArr = array();
            $qpUserArr = array();
            $qpSameUserArr = array();
            if (!empty($qpList)) {
                foreach ($qpList as $v) {
                    $qpAmountArr[] = $v['amount'];
                    if (in_array($v['user_id'], $qpUserArr)) {
                        if (!in_array($v['user_id'], $qpSameUserArr)) {
                            $qpSameUserArr[] = $v['user_id'];
                        }
                    } else {
                        $qpUserArr[] = $v['user_id'];
                    }
                }
            }

            //查询本月-台球-流水订单
            $tqOrder = new Shoptableorder();
            $tqList = $tqOrder->where($where)->where('status', 'in', ['doing', 'done'])->select();

            $tqAmountArr = array();
            $tqUserArr = array();
            $tqSameUserArr = array();
            if (!empty($tqList)) {
                foreach ($tqList as $n) {
                    $tqAmountArr[] = $n['amount'];
                    if (!in_array($n['user_id'], $tqUserArr)) {
                        $tqUserArr[] = $n['user_id'];
                    } else {
                        if (!in_array($n['user_id'], $tqSameUserArr)) {
                            $tqSameUserArr[] = $n['user_id'];
                        }
                    }
                }
            }

            // 棋牌流水
            $qpAmount = ceil(array_sum($qp) * 100) / 100;

            //台球流水
            $tqAmount = ceil(array_sum($tq) * 100) / 100;

            //棋牌和台球订单总流水
            $totalAmount = $qpAmount + $tqAmount;

            //小程序总收益
            $totalProfit = ceil(array_sum($profitDataArr) * 100) /100;

            //棋牌和台球订单总数
            $totalCounts = count($qpAmountArr) + count($tqAmountArr);

            //美团流水
            $totalMtAmount = ceil(array_sum($mt) * 100) / 100;

            //抖音流水
            $totalDouAmount = ceil(array_sum($dou) * 100) / 100;

            //棋牌复购占比
            if (count($qpUserArr) == 0) {
                $qpRepeatUserRatio = 0;
            } else {
                $qpRepeatUserRatio = ceil(count($qpSameUserArr) / count($qpUserArr) * 10000) / 100;
            }

            //台球复购占比
            if (count($tqUserArr) == 0) {
                $tqRepeatUserRatio = 0;
            } else {
                $tqRepeatUserRatio = ceil(count($tqSameUserArr) / count($tqUserArr) * 10000) / 100;
            }

            $data['qpAmount'] = $qpAmount;
            $data['tqAmount'] = $tqAmount;
            $data['totalAmount'] = $totalAmount;
            $data['totalProfit'] = $totalProfit;
            $data['totalCounts'] = $totalCounts;
            $data['totalMtAmount'] = $totalMtAmount;
            $data['totalDouAmount'] = $totalDouAmount;
            $data['qpRepeatUserRatio'] = $qpRepeatUserRatio;
            $data['tqRepeatUserRatio'] = $tqRepeatUserRatio;

            return json($data);
        }

        return $this->view->fetch();
    }

    //获取月份第一天0点时间戳
    protected function getFirstDayTimestamp($date)
    {
        return strtotime($date . '-01 00:00:00');
    }

    //获取月份最后一天23:59:59的时间戳
    protected function getLastDayTimestamp($date)
    {
        // 生成该月最后一天的日期字符串
        $lastDay = date("Y-m-t", strtotime($date . "-01"));
        // 获取该日期的23:59:59时间戳
        return strtotime($lastDay . " 23:59:59");
    }

    //获取自然月的日期数组
    protected function getDaysArray($year, $month)
    {
        $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        return range(1, $daysInMonth);
    }


}